import org.zkoss.zul.Area;
import org.zkoss.zul.Chart;
import org.zkoss.zul.Menupopup;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.SimplePieModel;
import org.zkoss.zul.Window;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;

import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.OpenEvent;

import java.util.Map;
import java.uitl.Iterator;

public class PieChart extends Chart {
	private Area selectedArea;												/*  Determine which area is selected  */
	private SimplePieModel pieModel;
	private Map dataMap;                                                    /*  Data passed into chart  */
	private boolean onArea;                                                 /*  Determine wheather an area is selected or not  */
	
	/*	Consturctor	*/
	public PieChart(){
		super();
		setType( "pie" );
		onArea = false;
		
		pieModel = new SimplePieModel();
		setModel( pieModel );                                               /*  Set chart model  */
		
		addEventListener( "onClick", new EventListener(){                   /*  Data dig  */
			public void onEvent( Event event ){
				Menupopup popup = ( Menupopup )( getParent().getFellow( "DG_PPOP" ) );
				if( event.getArea() != null ){
					selectedArea = getFellow( event.getArea() );
					onArea = true;	
					( ( Menuitem )popup.getFellow( "DG_PPOP_DETAIL" ) ).setDisabled( false );
					( ( Menuitem )popup.getFellow( "DG_PPOP_INSERT" ) ).setDisabled( true );
					( ( Menuitem )popup.getFellow( "DG_PPOP_MODIFY" ) ).setDisabled( false );
					( ( Menuitem )popup.getFellow( "DG_PPOP_REMOVE" ) ).setDisabled( false );
				}else{
					selectedArea = null;
					onArea = false;
				}
			}
		} );
	}
	
	/*  Show detail popup window  */
	public void detail(){
		final Window winDet = ( Window )Executions.createComponents( "/components/PieDetail.zul", null, null );
		Comparable cat = ( Comparable )selectedArea.getAttribute( "category" );
		( ( Label )winDet.getFellow( "DG_DECAT" ) ).setValue( cat );
		( ( Label )winDet.getFellow( "DG_DEVAL" ) ).setValue( pieModel.getValue( cat ).toString() );
		winDet.doHighlighted();
		selectedArea = null;
	}
	
	/*  Show insert popup window  */
	public void insertDialog(){
		final Window winIns = ( Window )Executions.createComponents( "/components/PieInsert.zul", null, null );
		winIns.setSizable( false );
		winIns.setAttribute( "commander", this );
		winIns.doModal();
	}
	
	/*  Show remove popup window  */
	public void remove(){
		Messagebox msg = new Messagebox();
		if( msg.show( "确定删除" + selectedArea.getAttribute( "category" ) + "吗?", "确定删除", Messagebox.YES + Messagebox.NO, Messagebox.EXCLAMATION ) == Messagebox.YES ){
			pieModel.removeValue( selectedArea.getAttribute( "category" ) );
			selectedArea = null;
			refreshDB();
		}		
	}
	
	/*  Insert data into pie chart  */
	public void insert(){
		Comparable com = ( ( Textbox )Path.getComponent( "/DG_PINSERT/DG_IM_CTB" ) ).getValue();
		Number num = new Double( ( ( Textbox )Path.getComponent( "/DG_PINSERT/DG_IM_VTB" ) ).getValue() );
		validateInput( com, num );
		pieModel.setValue( com, num );
		selectedArea = null;
		DG_PINSERT.detach();
		refreshDB();
	}
	
	/*  Show modify popup window  */
	public void modifyDialog(){
		final Window winMod = ( Window )Executions.createComponents( "/components/PieModify.zul", null, null );
		Comparable cat = ( Comparable )selectedArea.getAttribute( "category" );
		String val = pieModel.getValue( cat ).toString();
		( ( Label )winMod.getFellow( "DG_MOCAT" ) ).setValue( cat );
		( ( Label )winMod.getFellow( "DG_MOVAL" ) ).setValue( val );
		( ( Textbox )winMod.getFellow( "DG_MM_CTB" ) ).setValue( cat );
		( ( Textbox )winMod.getFellow( "DG_MM_VTB" ) ).setValue( val );
		winMod.setSizable( false );
		winMod.setAttribute( "commander", this );
		winMod.doHighlighted();
	}
	
	/*  Update pie chart  */
	public void modify(){
		Comparable com = ( ( Textbox )Path.getComponent( "/DG_PMODIFY/DG_MM_CTB" ) ).getValue();
		Number num = new Double( ( ( Textbox )Path.getComponent( "/DG_PMODIFY/DG_MM_VTB" ) ).getValue() );
		validateInput( com, num );
		pieModel.removeValue( selectedArea.getAttribute( "category" ) );
		pieModel.setValue( com, num );
		selectedArea = null;
		DG_PMODIFY.detach();
		refreshDB();
	}
	
	/*  Process popup menu open event  */
	public void openPopup( OpenEvent event){
		if( !event.isOpen() ){
			Menupopup popup = ( Menupopup )( getParent().getFellow( "DG_PPOP" ) );
			if( !onArea ){
				( ( Menuitem )popup.getFellow( "DG_PPOP_DETAIL" ) ).setDisabled( true );
				( ( Menuitem )popup.getFellow( "DG_PPOP_INSERT" ) ).setDisabled( false );
				( ( Menuitem )popup.getFellow( "DG_PPOP_MODIFY" ) ).setDisabled( true );
				( ( Menuitem )popup.getFellow( "DG_PPOP_REMOVE" ) ).setDisabled( true );
			}
		}else{
			onArea = false;
		}
	}
	
	/*  To be implemented  */
	private void validateInput( Comparable com, Number num ){
		
	}
	
	/*  To be implemented  */
	private void refreshDB(){
		
	}
	
	public void setVariable( String name, Object value, boolean local ){
		if( name.equals( "data" ) ){		
			dataMap = ( Map )value;
			Iterator keyIter = dataMap.keySet().iterator();
			Iterator valueIter = dataMap.values().iterator();
			while( keyIter.hasNext() ){
				pieModel.setValue( keyIter.next(), valueIter.next() );
			}
			return;
		}else
			super.setVariable( name, value, local );
	}
}